<template>
  <div id="login">
    <Particles class="particles"></Particles>
    <div class="loginbox">
      <div class="logo">星空服饰POS系统</div>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="form"
      >
       <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off" @keyup.enter.native="login('ruleForm')"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="login('ruleForm')">登录</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import Particles from "@/components/particles/Particles";
import axios from 'axios';
const baseUrl = "http://47.102.99.195:8888";
export default {
  name: "login",
  data() {
    var checkUsername = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("用户名不能为空"));
      } else {
        if (this.ruleForm.username !== "") {
          this.$refs.ruleForm.validateField("checkUsername");
        }
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        pass: "",
        checkPass: "",
        username: ""
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        username: [{ validator: checkUsername, trigger: "blur" }]
      }
    };
  },
  components: {
    Particles
  },
  mounted() {},
  methods: {
    login(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          //请求后台数据
          var map = {
            username:"",
            password:""
          }
          map.username = this.ruleForm.username;
          map.password = this.ruleForm.pass;
          axios.post(baseUrl+"/user/login",map).then(res=>{
            console.log(res.data);

            if(res.data.event===0){
               this.$message({
                type:"success",
                message:"登录成功"
              });
              window.localStorage.setItem('username', this.ruleForm.username);//设置用户名
              this.$router.push('/home');
            }
            if(res.data.event===-1){
              this.$message({
            type:"error",
            message:res.data.msg
          });
            }
          });
        } else {
          this.$message({
            type:"error",
            message:"登录失败"
          });
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>
<style lang="scss" scoped>
.particles {
  width: 100%;
  height: 100%;
  position: absolute;
}
.loginbox {
  height: 300px;
  width: 400px;
  background-color: azure;
  background-color: rgba(0, 0, 0, 0.5);
  position: relative;
  margin: auto;
  top: 200px;
  bottom: 0;
  left: 0;
  right: 0;
  padding-top: 25px;
  // padding-left: 20px;
  padding-bottom: 10px;
  padding-right: 40px;
}
.logo{
  font-size: 30px;
  margin-bottom: 10px;
  margin-left: 28%;
  color: azure;
  opacity: 0.5;
  font-family: 'Lucida Sans';
}
</style>
